package work.timejay.exception;

import lombok.Data;
import lombok.EqualsAndHashCode;
import work.timejay.enums.ErrorMsg;

import java.util.Arrays;
import java.util.Collections;

/**
 * @className:TimeJayException.java
 * @author:王文杰
 * @version:1.0.0
 * @description:业务异常类
 * @createTime 2023年11月22日 10:37:00
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class TimeJayException extends RuntimeException {
    private String code;
    private Object[] params;

    private static final String FAIL = "FAIL";

    public TimeJayException(String message) {
        super(message);
    }

    public static TimeJayException fail(String code, Object... msg) {
        TimeJayException bean = new TimeJayException(FAIL);
        bean.setCode(code);
        bean.setParams(msg);
        return bean;
    }
    public static TimeJayException fail(ErrorMsg errorMsg) {
        TimeJayException bean = new TimeJayException(FAIL);
        bean.setCode(errorMsg.getCode());
        bean.setParams(Collections.singletonList(errorMsg.getMsg()).toArray());
        return bean;
    }
}
